import json
import uuid

from conf.neo4j_cnx import NeoCnxPool

neo_cnx = NeoCnxPool()


class PropSetUp:

    def update_prop(self, node_properties_item: dict, rel_properties_item: dict):
        with neo_cnx.get_session() as session:
            node_properties_item = json.dumps(node_properties_item, ensure_ascii=False)
            rel_properties_item = json.dumps(rel_properties_item, ensure_ascii=False)
            if self.does_node_exists():
                query = (
                    'MATCH (n:Prop) '
                    'SET n.node_properties_item = $node_properties_item, '
                    'n.rel_properties_item = $rel_properties_item'
                )
                session.run(query,
                            node_properties_item=node_properties_item,
                            rel_properties_item=rel_properties_item)
            else:
                query = (
                    'MERGE (n:Prop { '
                    'node_properties_item: $node_properties_item, '
                    'rel_properties_item:$rel_properties_item, '
                    'uid: $uid })'
                )
                session.run(query, node_properties_item=node_properties_item,
                            rel_properties_item=rel_properties_item, uid=str(uuid.uuid4()))

    def does_node_exists(self):
        with neo_cnx.get_session() as session:
            query = (
                'MATCH (n:Prop) '
                'RETURN count(n) AS count'
            )
            results = session.run(query)
            results = [record['count'] for record in results][0]
            if results > 0:
                return True
            else:
                return False

    def query_prop(self):
        with neo_cnx.get_session() as session:
            query = (
                'MATCH (n:Prop) '
                'RETURN n.node_properties_item AS node_properties_item, '
                'n.rel_properties_item AS rel_properties_item'
            )
            results = session.run(query)
            for record in results:
                return {
                    'node_properties_item': json.loads(record['node_properties_item']),
                    'rel_properties_item': json.loads(record['rel_properties_item']),
                }


node_properties_item = PropSetUp().query_prop()['node_properties_item']
rel_properties_item = PropSetUp().query_prop()['rel_properties_item']


def prop_view(properties: dict, type='NODE'):
    if type == 'NODE':
        for old_key in properties.copy().keys():
            if old_key in node_properties_item:
                new_key = node_properties_item[old_key]
                properties[new_key] = properties.pop(old_key)
                if properties[new_key] == '':
                    properties.pop(new_key)
            else:
                properties.pop(old_key)
    else:
        for old_key in properties.copy().keys():
            if old_key in rel_properties_item:
                new_key = rel_properties_item[old_key]
                properties[new_key] = properties.pop(old_key)
                if properties[new_key] == '':
                    properties.pop(new_key)
            else:
                properties.pop(old_key)
    return properties


def prop_model(properties: dict, type='NODE'):
    if type == 'NODE':
        for old_key in properties.copy().keys():
            for key, value in node_properties_item.items():
                if old_key == value:
                    new_key = key
                    properties[new_key] = properties.pop(old_key)
    else:
        for old_key in properties.copy().keys():
            for key, value in rel_properties_item.items():
                if old_key == value:
                    new_key = key
                    properties[new_key] = properties.pop(old_key)
    return properties


if __name__ == '__main__':
    PropSetUp().update_prop(node_properties_item={'name': '名称',
                                                  'code': '编码'},
                            rel_properties_item={'importance': '重要性',
                                                 'freq': '频率'})
    data = PropSetUp().query_prop()
    data = prop_view({'name': '肺结核', 'code': 'X123', 'class_kind': 'category'})
    data = prop_model({'名称': '肺结核', '编码': 'X123'})
